<template>
  <div class="cardWrapper" :id="`menuScroll${scrollFirstIndex}-${componentsIndex}`" v-if="layerItem.children.length">
    <div class="cardTitleContainer">
      <img src="https://img3.job1001.com/data_center/images/position_item_icon.png" alt="Icon"
        class="position_item_icon" />
      <div class="cardTitle">{{ layerItem.title }}</div>
    </div>
    <div class="layerList">
      <el-row class="demo-autocomplete" :key="index" v-for="item,index in layerItem.children||layerList">
        <div class="demo-autocomplete-content">
          <div class="controlLine" :id="`scroll${componentsIndex}-${index}`">
            <span class="tinyTitle">
              <div class="titleFlag"></div>
              {{ item.title }}
            </span>
            <div class="line"></div>
          </div>

          <!-- 以上是标题，以下是内容，目前是只改变内容就好了 -->
          <!-- 工作经验要求 -->
          <div v-if="item.layerId==191" v-loading="loading.general">
            <div class="chartContainer">
              <pieChart :chartData="generalEducationPieData" :layerId="item.layerId" />
            </div>
          </div>
          <!-- 城市工作经验要求 -->
          <div v-if="item.layerId==188" v-loading="loading.city">
            <el-form ref="form" :model="form.city">
              <el-form-item label="区域名称:" label-width="100px">
                <regoinSearchNoCountry ref="regionRef" @onRegionChange="onRegionChange" />
              </el-form-item>
            </el-form>
            <div class="chartContainer">
              <pieChart :chartData="cityEducationPieData" colorsetIndex="1" :layerId="item.layerId"/>
            </div>
          </div>
          <!-- 行业工作经验要求 -->
          <div v-if="item.layerId==189" v-loading="loading.industry">
            <el-form ref="form" :model="form.industry" label-width="100px">
              <el-form-item label="行业名称：" label-width="100px">
                <IndustryCheckbox :limitNum="1" ref="industryRef" :selectIndustryType="4" @onIndustryChange="onIndustryChange" />
              </el-form-item>
            </el-form>
            <div class="chartContainer">
              <pieChart :chartData="industryEducationPieData" colorsetIndex="2" :layerId="item.layerId"/>
            </div>
          </div>
          <!-- 企业类型工作经验要求 -->
          <div v-if="item.layerId==190" v-loading="loading.company">
            <el-form ref="form" :model="form.company">
              <el-form-item label="企业规模:">
                <companyCheckbox :limitNum="1" ref="companyRef" @onCompanyChange="onCompanyChange" />
              </el-form-item>
            </el-form>
            <div class="chartContainer">
              <pieChart :chartData="companyEducationPieData" colorsetIndex="0" :layerId="item.layerId"/>
            </div>
          </div>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import regoinSearchNoCountry from "@/views/position/components/regoinSearchNoCountry";
import IndustryCheckbox from "@/components/IndustryCheckbox/index";
import companyCheckbox from "@/components/companyCheckbox/index";
import pieChart from "@/components/charts/pieChart.vue";
import { reqChartPieData } from "@/api/chart.js";
export default {
  name: "workDemand",
  components: {
    pieChart,
    regoinSearchNoCountry,
    IndustryCheckbox,
    companyCheckbox,
  },
  props: {
    layerList: {
      type: Array,
      default: () => [],
    },
    layerItem: {
      type: Object,
      default: () => {},
    },
    topId: {
      type: String,
      required: true,
    },
    isSuperToken: {
      type: Boolean,
      default: false,
    },
    scrollFirstIndex: {
      type: [String, Number],
      default: 0,
    },
    aiCreateStatus: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    layerItem: {
      handler(newValue, oldValue) {
        if (newValue && newValue.layerId) {
          this.layerList.forEach((item, index) => {
            if (item.layerId == newValue.layerId) {
              this.componentsIndex = index;
            }
          });
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      generalEducationPieData: {},
      cityEducationPieData: {},
      industryEducationPieData: {},
      companyEducationPieData: {},
      positionId: this.$route.query.id,
      componentsIndex: "", //用来标记组件下标，滚动用
      form: {
        city: {},
        inducstry: {},
        company: {},
      },
      loading: {
        general: false,
        city: false,
        industry: false,
        company: false,
      },
      selectedRegionList: [],
      selectedIndustryList: [],
      selectedCompanyList: [],
      currentChartIndustryName: "",
      currentChartCompanyName: "",
    };
  },
  created() {
    this.getGeneralPieData();
  },
  methods: {
    // 为了防止以后会出现不同情况，所以就不过多耦合代码
    getGeneralPieData() {
      this.loading.general = true;
      const params = {
        ylPositionId: this.positionId,
        keyType: "-1", //-1全部 0 城市1:国标行业 2:企业规模
        keyId: "",
        analyzeType: 1, //0:学历要求维度 1:工作经验维度
      };
      reqChartPieData(params)
        .then((res) => {
          if (res.code == 200) {
            const temp = {
              descValue: 0,
              maxRateRange: 0,
              maxRate: 0,
              list: [],
            };
            res.data.forEach((item, index) => {
              temp.list.push({
                name: item.analyzeName,
                value: item.percent,
              });
              if (temp.maxRate < item.percent) {
                temp.maxRate = item.percent;
              }
            });
            this.generalEducationPieData = temp;
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading.general = false;
        });
    },
    getCityPieData() {
      this.loading.city = true;
      const params = {
        ylPositionId: this.positionId,
        keyType: "0", //-1全部 0 城市1:国标行业 2:企业规模
        keyId: this.selectedRegionList.join(""),
        analyzeType: 1, //0:学历要求维度 1:工作经验维度
      };
      reqChartPieData(params)
        .then((res) => {
          if (res.code == 200) {
            const temp = {
              descValue: 0,
              maxRateRange: 0,
              maxRate: 0,
              list: [],
            };
            res.data.forEach((item, index) => {
              temp.list.push({
                name: item.analyzeName,
                value: item.percent,
              });
              if (temp.maxRate < item.percent) {
                temp.maxRate = item.percent;
              }
            });
            this.cityEducationPieData = temp;
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading.city = false;
        });
    },
    getIndustryPieData() {
      this.loading.industry = true;
      const params = {
        ylPositionId: this.positionId,
        keyType: "1", //-1全部 0 城市1:国标行业 2:企业规模
        keyId: this.selectedIndustryList.join(""),
        analyzeType: 1, //0:学历要求维度 1:工作经验维度
      };
      reqChartPieData(params)
        .then((res) => {
          if (res.code == 200) {
            const temp = {
              descValue: 0,
              maxRateRange: 0,
              maxRate: 0,
              list: [],
            };
            res.data.forEach((item, index) => {
              temp.list.push({
                name: item.analyzeName,
                value: item.percent,
              });
              if (temp.maxRate < item.percent) {
                temp.maxRate = item.percent;
              }
            });
            this.industryEducationPieData = temp;
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading.industry = false;
        });
    },
    getCompanyPieData() {
      this.loading.company = true;
      const params = {
        ylPositionId: this.positionId,
        keyType: "2", //-1全部 0 城市1:国标行业 2:企业规模
        keyId: this.selectedCompanyList.join(""),
        analyzeType: 1, //0:学历要求维度 1:工作经验维度
      };
      reqChartPieData(params)
        .then((res) => {
          if (res.code == 200) {
            const temp = {
              descValue: 0,
              maxRateRange: 0,
              maxRate: 0,
              list: [],
            };
            res.data.forEach((item, index) => {
              temp.list.push({
                name: item.analyzeName,
                value: item.percent,
              });
              if (temp.maxRate < item.percent) {
                temp.maxRate = item.percent;
              }
            });
            this.companyEducationPieData = temp;
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading.company = false;
        });
    },
    onRegionChange(val) {
      this.selectedRegionList = [val.value];
      this.getCityPieData();
    },
    onIndustryChange(val) {
      if (val && val.length) {
        this.currentChartIndustryName = val[0].split("-")[1];
        this.selectedIndustryList = [];
        val.forEach((item) => {
          this.selectedIndustryList.push(item.split("-")[0]);
        });
      } else {
        this.currentChartIndustryName = "";
        this.selectedIndustryList = [];
      }
      this.getIndustryPieData();
    },
    onCompanyChange(val) {
      if (val && val.length) {
        this.currentChartCompanyName = val[0].split("-")[1];
        this.selectedCompanyList = [];
        val.forEach((item) => {
          this.selectedCompanyList.push(item.split("-")[0]);
        });
      } else {
        this.currentChartCompanyName = "";
        this.selectedCompanyList = [];
      }
      this.getCompanyPieData();
    },
  },
};
</script>

<style lang="scss" scoped>
.cardWrapper {
  // position: relative;
  min-width: 800px;
  border-radius: 10px;
  border: 1px solid #cbdcfe;
  background-size: 110% 219px;
  background-position: -10px -10px;
  background-image: url("https://img3.job1001.com/data_center/images/salary_bg.png");
  background-repeat: no-repeat;
  padding: 20px;
  flex: 1;
  margin: 0;

  margin-bottom: 14px;
  margin-top: 20px;
  &::-webkit-scrollbar {
    width: 4px;
    background-color: #fff;
    border-radius: 4px;

    height: 4px;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #d8d8d8;
    border-radius: 4px;
  }
  .cardTitleContainer {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    // position: sticky;
    top: 0;
    .cardTitle {
      height: 30px;
      font-size: 22px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #000000;
      line-height: 30px;
      margin-right: 15px;
    }
    .introduceJobInput {
      width: 400px;
    }
    .introduceJob {
      height: 21px;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 21px;
      margin-right: 38px;
    }
    .position_item_icon {
      width: 39px;
      height: 40px;
      margin-right: 13px;
    }
  }

  .layerList {
    .demo-autocomplete {
      display: flex;
      .demo-autocomplete-content {
        flex: 1;
      }
      .positionPic {
        width: 140px;
        // height: 137px;
        border-radius: 16px;
        opacity: 0.77;
      }
    }
  }

  // 以下是通用样式
  .contentItemEditAndAIBtn {
    line-height: 32px;
    margin: 0 10px 0 15px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #888888;
    display: flex;
    align-items: center;
    cursor: pointer;
    img {
      width: 22px;
      height: 22px;
    }
  }
  .titleFlag {
    width: 5px;
    height: 15px;
    border-radius: 3px;
    background: #4880ff;
    margin-right: 12px;
  }
  .controlLine {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 15px;
    .tinyTitle {
      display: flex;
      align-items: center;
      font-weight: bold;
      font-size: 18px;
      // margin-right: 24px;
    }
    .line {
      flex: 1;
      // height: 16px;
      margin-left: 14px;
      border-bottom: 1px solid #efefef;
    }
    .saveBtns {
      display: flex;
      height: 32px;
      align-items: center;
      justify-content: center;
      // padding-right: 10px;
      .contentItemEditAndAIBtn {
        line-height: 32px;
        margin: 0 10px 0 15px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #888888;
        display: flex;
        align-items: center;
        cursor: pointer;
        img {
          width: 22px;
          height: 22px;
        }
      }
    }
    .AILoading {
      position: relative;
      &::after {
        content: "";
        position: absolute;
        top: 0;
        right: 0;
        width: 0px;
        height: 100%;
        background-color: transparent;
        animation-name: ellipsis;
        animation-duration: 1.5s;
        animation-iteration-count: infinite;
      }
    }
  }
  .editBox {
    width: 100%;
    display: flex;
    // padding-right: 20px;
    margin-bottom: 52px;

    .otherNameContainer {
      display: flex;
      flex-wrap: wrap;
      max-width: 50%;
      align-content: flex-start;
      flex: 1;
      .otherName {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 38px;
        border-radius: 4px;
        border: 1px solid #b2bad4;
        padding: 7px 18px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        line-height: 24px;
        margin-right: 18px;
        margin-bottom: 24px;
      }
    }
    .similerContainer {
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      max-width: 50%;
      align-content: flex-start;
      .similer {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 38px;
        background: #edf4ff;
        border-radius: 6px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        line-height: 24px;
        margin-right: 18px;
        padding: 7px 18px;
        margin-bottom: 24px;
      }
    }
    .editBoxLeft {
      flex: 1;
      position: relative;
      border: 1px solid #709cff;
      background-color: white;
      border-radius: 6px 0px 0px 6px;

      .deleteContent {
        position: absolute;
        cursor: pointer;
        right: 12px;
        top: 15px;
      }
      .fakeTextArea {
        position: relative;
        background-color: white;
        min-height: 475px;
        padding: 14px;
        .listContentItem {
          // background-color: gray;
          position: relative;
          border-radius: 4px;
          margin-bottom: 29px;
          textarea {
            position: relative;
            background-color: #eeeeee;
            font-size: 15px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #555555;
            line-height: 26px;
            padding: 4px 5px 0px 6px;
            height: 100% !important;
          }
          .deleteListContent {
            position: absolute;
            display: flex;
            justify-content: flex-end;
            right: 0;
            width: 100%;
            background-color: #eeeeee;
            padding-right: 6px;
            padding-bottom: 8px;
            img {
              cursor: pointer;
            }
          }
        }
        .addListContentBtn {
          width: 100%;
          height: 32px;
          background: #ffffff;
          border-radius: 4px;
          border: 1px solid #4880ff;
          color: #4880ff;
          cursor: pointer;
        }
      }
    }
    .editBoxRight {
      flex: 1;
      padding: 0px 16px 0px 16px;
      background: #f5f9ff;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #4b6aaf;
      line-height: 27px;
      position: relative;
      padding-bottom: 20px;
      border-radius: 0px 6px 6px 0px;
      .extendsItem {
        position: relative;
        padding-bottom: 35px;
        padding: 0px 16px;
        .replaceByOne {
          display: flex;
          justify-content: flex-end;
          position: relative;
          bottom: 0;
          right: 14px;
          cursor: pointer;
          color: #4880ff;
        }
      }
    }
    .noEdit {
      // padding: 0;
      background: transparent;
    }
    .playlistContainer {
      display: flex;
      // width: 251px;
      height: 160px;
      overflow-x: auto;
      overflow-y: hidden;
      margin-left: 47px;
      .playlistItem {
        position: relative;
        width: 217.64px;
        height: 137px;
        margin-right: 10px;
        cursor: pointer;
        &::after {
          content: "";
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 41px;
          background: linear-gradient(
            180deg,
            rgba(0, 0, 0, 0) 0%,
            rgba(0, 0, 0, 0.3) 100%
          );
          border-radius: 0px 0px 15px 15px;
          z-index: 1;
        }
        img {
          width: 217.64px;
          height: 137px;
          border-radius: 16px;
        }
        .playlistItemName {
          position: absolute;
          bottom: 11px;
          left: 0;
          padding: 0 13px;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 16px;
          color: #fff;
          line-height: 22px;
          text-align: left;
          font-style: normal;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-inline-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
          z-index: 2;
        }
      }
      &::-webkit-scrollbar-track-piece {
        background: #dee6ec;
      }

      &::-webkit-scrollbar {
        height: 6px;
      }

      &::-webkit-scrollbar-thumb {
        background: #bccad5;
        border-radius: 20px;
      }
    }
  }
  .aiContent {
    flex: 1;
    height: 100%;
    font-size: 15px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #555555;
    line-height: 27px;
    // max-width: 90%;
    word-break: break-general;
    white-space: normal;
  }
  .saveGeneralBtn {
    width: 57px;
    height: 26px;
    border: none;
    background: #4880ff;
    color: #ffffff;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    border-radius: 4px;
    margin-left: 15px;
    margin-right: 20px;
    cursor: pointer;
  }
}

::v-deep .el-form-item {
  display: flex;
  align-items: center;
}
::v-deep .el-form-item__label {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 27px;
  min-width: 68px !important;
}
::v-deep .el-form-item__content {
  margin-left: 0 !important;
}
</style>